//
//  MyController.m
//  MyBaseProject
//
//  Created by zsl on 15/12/6.
//  Copyright © 2015年 zsl. All rights reserved.
//

#import "MyController.h"
#import "SetController.h"
#import "RegisterLoginController.h"

@interface MyController () <UITableViewDataSource, UITableViewDelegate>
@property (strong, nonatomic) UIView *headView;
@property (strong, nonatomic) UITableView *tableView;
@property (strong, nonatomic) UIButton *myBtn;

@property (nonatomic)BOOL isLogin;


@end

@implementation MyController

- (NSUserDefaults *)userDefault {
    if (!_userDefault) {
        _userDefault = [NSUserDefaults standardUserDefaults];
        ;
    }
    return _userDefault;
}

- (instancetype)init {
    if (self = [super init]) {
        [self headView];
        [self tableView];
    }
    return self;
}

+ (UINavigationController *)defaultMyNavi {
        MyController *vc = [MyController new];
        UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:vc];
    return navi;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kRGBColor(244, 244, 244);
    self.title = @"个人";
    
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //self.navigationController.navigationBar.hidden = YES;
    self.tabBarController.tabBar.hidden = NO;
    NSString *path = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES).firstObject;
    NSLog(@"%@",path);
    NSLog(@"%d",[self.userDefault boolForKey:@"isLog"]);
    self.isLogin = [self.userDefault boolForKey:@"isLog"];
    if ( self.isLogin != YES) {
        self.label.text = @"注册/登陆";
        [self.myBtn setBackgroundImage:[UIImage imageNamed:@"loginview_username"] forState:UIControlStateNormal];
        
    } else {
        self.label.text = [self.userDefault stringForKey:@"userName"];
        [self.myBtn setBackgroundImage:[UIImage imageNamed:@"lufei"] forState:UIControlStateNormal];
    }
   
    
}


#pragma mark - <UITableViewDataSource>
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.textLabel.font = kTitleFont;
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //cell.imageView.image = [UIImage imageNamed:@"me_setting"];

    cell.textLabel.text = @"设置";
    return cell;
}

#pragma mark - <UITableViewDelegate>
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SetController *vc = [SetController new];
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

#pragma mark - 懒加载
- (UIView *)headView {
    if(_headView == nil) {
        _headView = [[UIView alloc] init];
        UIImageView *imgView = [UIImageView new];
        //imgView.image = [UIImage imageNamed:@"MyBackgroud"];
        imgView.backgroundColor = kHeaderImageBackColor;
        [_headView addSubview:imgView];
        [imgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(0);
        }];
        
        
        [self.myBtn bk_addEventHandler:^(id sender) {
            if ([self.label.text isEqualToString:@"注册/登陆"]) {
                RegisterLoginController *vc = [RegisterLoginController new];
                vc.hidesBottomBarWhenPushed = YES;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                return ;
            }
        } forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_headView];
        [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.left.right.mas_equalTo(0);
            make.height.mas_equalTo(kWindowH/3);
        }];

        
    }
    return _headView;
}

- (UITableView *)tableView {
    if(_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.bottom.right.mas_equalTo(0);
            make.top.mas_equalTo(self.headView.mas_bottom).mas_equalTo(0);
        }];
    }
    return _tableView;
}


- (UILabel *)label {
    if(_label == nil) {
        _label = [[UILabel alloc] init];
        _label.text = @"注册/登陆";
        _label.font = kSubTitleFont;
        _label.textColor = [UIColor whiteColor];
        _label.textAlignment = NSTextAlignmentCenter;
        [_headView addSubview:self.label];
        [_label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.myBtn);
            make.width.mas_equalTo(65);
            make.top.mas_equalTo(self.myBtn.mas_bottom).mas_equalTo(5);
        }];
        
        
    }
    return _label;
}


- (UIButton *)myBtn {
	if(_myBtn == nil) {
		_myBtn = [[UIButton alloc] init];
        [_headView addSubview:self.myBtn];
        [_myBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.mas_equalTo(0);
            make.size.mas_equalTo(CGSizeMake(65, 65));
        }];
	}
	return _myBtn;
}


@end
